package cn.weekdragon.gscore.adapter;

import cn.weekdragon.gscore.adapter.bot.Bot;
import cn.weekdragon.gscore.adapter.filter.BotFilter;
import cn.weekdragon.gscore.adapter.model.EventExt;
import cn.weekdragon.gscore.adapter.model.enums.EventEnum;
import cn.weekdragon.gscore.adapter.model.message.Message;
import cn.weekdragon.gscore.adapter.route.Route;
import cn.weekdragon.gscore.adapter.route.RouteManager;
import cn.weekdragon.gscore.adapter.service.Service;
import cn.weekdragon.gscore.adapter.service.ServiceManager;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Slf4j
public class EventChannel {

    private static final ThreadPoolExecutor TPE = new ThreadPoolExecutor(4, 16, 1, TimeUnit.MINUTES, new LinkedBlockingDeque<>(1000));


    public static void receiveMessage(Bot bot, String message) {
        TPE.submit(() -> {
            try {
                EventExt ext = bot.prepareEventExt(message);
                if (Objects.nonNull(ext)) {
                    EventExt.setCurrent(ext);
                    String botName = ext.getBot().getBotName();
                    List<Route> routes =  RouteManager.getServices(botName);
                    for (Route route : routes) {
                        if(route.getPredicates().stream().anyMatch(e->!e.test(ext))){
                            continue;
                        }
                        Optional<Service> serviceOptional = ServiceManager.getService(route.getService());
                        serviceOptional.ifPresent(service -> {
                            List<BotFilter> botFilters = route.getBotFilters();
                            for (BotFilter botFilter : botFilters) {
                                try {
                                    // 过滤器返回了消息，终端流程
                                    if(botFilter.doFilter(ext)){
                                        if(Objects.nonNull(ext.getOutMessage())){
                                            EventExt.autoMessage(ext.getOutMessage());
                                        }
                                        return;
                                    }
                                } catch (Exception e) {
                                    log.error("过滤器工作出错：", e);
                                }
                            }
                            try {
                                service.receiveMessage(ext);
                            }catch (Exception e){
                                log.error("群聊消息消费失败,adapter:{}",service.getClass().getSimpleName(),e);
                            }
                        });
                    }
                }
            } catch (Exception e) {
                log.error("处理消息失败:", e);
            } finally {
                EventExt.clear();
            }
        });
    }

}
